package com.tbit.uqbike.service.business.impl;

import com.github.pagehelper.PageHelper;
import com.tbit.uqbike.object.pojo.Account;
import com.tbit.uqbike.object.pojo.MoneyData;
import com.tbit.uqbike.object.pojo.UserAccountLog;
import com.tbit.uqbike.service.business.UserAccountLogService;
import com.tbit.uqbike.webmanager.dao.core.AccountDao;
import com.tbit.uqbike.webmanager.dao.core.UserAccountLogDao;
import com.tbit.uqbike.webmanager.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

@Service("userAccountLogService")
public class UserAccountLogServiceImpl implements UserAccountLogService {
    @Autowired
    private UserAccountLogDao userAccountLogDao;
    @Autowired
    private AccountDao accountDao;

    @Override
    public Integer queryCount(Integer accountId, List<Integer> userIds, Integer type) {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("accountId", accountId);
        params.put("userIds", userIds);
        params.put("type", type);

        return userAccountLogDao.queryCount(params);
    }

    @Override
    public List<UserAccountLog> queryPage(Integer accountId, List<Integer> userIds, Integer type, Integer rowCount, Integer pageNO) {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("accountId", accountId);
        params.put("userIds", userIds);
        params.put("type", type);
        params.put("start", (pageNO - 1) * rowCount);
        params.put("rowCount", rowCount);

        return userAccountLogDao.queryPage(params);
    }

    @Override
    public void insert(UserAccountLog userAccountLog) {
        userAccountLogDao.insert(userAccountLog);
    }

    @Override
    public List<UserAccountLog> getPage(Integer accountId,
                                        Long startTime, Long endTime, Integer pageNO, Integer rowCount) {
        Map<String, Object> params = new HashMap<>();
        params.put("accountId", accountId);
        if (startTime != null) {
            params.put("startTime", new Date(startTime));
        }
        if (endTime != null) {
            params.put("endTime", new Date(endTime));
        }
        PageHelper.startPage(pageNO, rowCount);
        return userAccountLogDao.getPage(params);
    }

    @Override
    public Integer getCount(Integer accountId, Long startTime,
                            Long endTime) {
        Map<String, Object> params = new HashMap<>();
        List<Integer> accountIds = getAccountId(accountId);
        params.put("accountIds", StringUtil.getListString(accountIds));
        if (startTime != null) {
            params.put("startTime", new Date(startTime));
        }
        if (endTime != null) {
            params.put("endTime", new Date(endTime));
        }
        return userAccountLogDao.getCount(params);
    }

    @Override
    public List<UserAccountLog> export(Integer accountId, Integer moneyType, Integer type,
                                       Long startTime, Long endTime) {
        Map<String, Object> params = new HashMap<>();
        List<Integer> accountIds = getAccountId(accountId);
        params.put("accountIds", StringUtil.getListString(accountIds));
        params.put("moneyType", moneyType);
        params.put("type", type);
        if (startTime != null) {
            params.put("startTime", new Date(startTime));
        }
        if (endTime != null) {
            params.put("endTime", new Date(endTime));
        }
        return userAccountLogDao.export(params);
    }

    @Override
    public MoneyData getSum(Integer accountId, Long startTime,
                            Long endTime) {
        Map<String, Object> params = new HashMap<>();
        List<Integer> accountIds = getAccountId(accountId);
        params.put("accountIds", StringUtil.getListString(accountIds));
        if (startTime != null) {
            params.put("startTime", new Date(startTime));
        }
        if (endTime != null) {
            params.put("endTime", new Date(endTime));
        }
        return userAccountLogDao.getSum(params);
    }

    /**
     * 获取品牌旗下代理
     *
     * @param accountId
     * @return
     */
    public List<Integer> getAccountId(Integer accountId) {
        List<Integer> list = new ArrayList<>();
        List<Account> accounts = accountDao.getById(accountId);
        if (accounts.size() > 0) {
            for (Account a : accounts) {
                list.add(a.getAccountId());
            }
        }
        /**品牌id也算进去*/
        list.add(accountId);
        return list;
    }

}